package com.fbinggun.dt.player;

import com.alibaba.fastjson.JSONObject;
import com.fbinggun.dt.DailyTask;
import com.fbinggun.dt.gui.ChoiceProject;
import com.fbinggun.dt.task.Task;
import com.fbinggun.dt.task.conditions.Conditions;
import org.bukkit.entity.Player;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class PlayerData {
    int taskCount;
    PlayerTask task;
    Map<ChoiceProject,String> choiceProject = new HashMap<>();
    String playerName;

    public PlayerData(Player player) throws SQLException, ClassNotFoundException {
        this.playerName = player.getName();
        initialChoice(player);
    }

    public PlayerData(Player player, JSONObject json) throws SQLException, ClassNotFoundException {
        this.playerName = player.getName();
        taskCount = json.getInteger("TaskCount");
        JSONObject choiceJson = json.getJSONObject("choiceProject");
        if(choiceJson==null){
        initialChoice(player);}else {
            for(String key:choiceJson.keySet()){
                ChoiceProject cp = DailyTask.getChoiceProject(key);
                if(cp!=null){
                    choiceProject.put(cp,choiceJson.getString(key));
                }
            }
        }
        JSONObject taskJson = json.getJSONObject("PlayerTask");
        if(taskJson!=null){
            task=new PlayerTask(taskJson);
        }
    }

    public JSONObject toJson(){
        JSONObject json = new JSONObject();
        json.put("PlayerName",playerName);
        json.put("TaskCount",taskCount);
       JSONObject choiceJson = new JSONObject();
       if(choiceProject!=null&&choiceProject.size()!=0){
           for(ChoiceProject cp:choiceProject.keySet()){
               choiceJson.put(cp.getId(),choiceProject.get(cp));
           }
           json.put("choiceProject",choiceJson);
       }

       if(task!=null){
          json.put("PlayerTask",task.toJson());
       }
        return json;
    }
    public void initialChoice(Player player) throws SQLException, ClassNotFoundException {
        choiceProject=new HashMap<>();
        if(DailyTask.choiceGui.choiceProjectHashMap!=null){
            for(ChoiceProject cp:DailyTask.choiceGui.choiceProjectHashMap.values()){
                if(player.hasPermission(cp.getPermission())) {
                    Task task1 = cp.getRandomTask();
                    DailyTask.debug("初始化玩家任务选择界面:选项 "+cp.getId());
                    DailyTask.debug("初始化玩家任务选择界面:任务 "+task1);
                    if(task1!=null) {
                        choiceProject.put(cp, task1.getId());
                    }
                }else {
                    choiceProject.put(cp, null);
                }
            }
        }
        save();
    }


    public int getTaskCount(){
        return taskCount;
    }
    public void addTaskCount(){
        taskCount=taskCount+1;
    }

    public boolean isGetTask(Player player){
        if(task!=null){
            return false;
        }
        if(taskCount<= DailyTask.plugin.getCount(player)){
            return true;
        }
        return false;
    }
    public void addCount(Conditions conditions, double a,Player player) throws SQLException, ClassNotFoundException {
        if(task==null){
            return;
        }
       task.addCount(conditions,a);
        if(task.getTask().isFinish(this)){
            task.getTask().reward(player);
        }
        save();
    }
    public void addCount(String id,double a,Player player) throws SQLException, ClassNotFoundException {
        if(task==null){
            return;
        }
        task.addCount(id,a);
        if(task.getTask().isFinish(this)){
            task.getTask().reward(player);
        }
        save();
    }

    public void removeTask() throws SQLException, ClassNotFoundException {
        task=null;
        save();
    }
    public PlayerTask getTask() {
        return task;
    }

    public double getCount(String id){
        if(task==null){
            return 0;
        }
        return task.getCount(id);
    }
    public double getCount(Conditions conditions){
        if(task==null){
            return 0;
        }
        return task.getCount(conditions);
    }

    public void setCount(Conditions conditions,double a,Player player) throws SQLException, ClassNotFoundException {
        if(task==null){
            return;
        }
       task.setCount(conditions,a);
        if(task.getTask().isFinish(this)){
            task.getTask().reward(player);
        }
        save();
    }
    public void setCount(String id,double a,Player player) throws SQLException, ClassNotFoundException {
        if(task==null){
            return;
        }
        task.setCount(id,a);
        if(task.getTask().isFinish(this)){
            task.getTask().reward(player);
        }
        save();
    }

    public void setTask(Task task) throws SQLException, ClassNotFoundException {
        this.task= new PlayerTask(task,task.getTimeLimit());
        addTaskCount();
        save();
    }
    public boolean isOverdue(){
        if(task==null){
            return true;
        }
       return task.isOverdue();
    }
    public Task getChoiceTask(ChoiceProject choiceProject){
        String taskId =this.choiceProject.get(choiceProject);
        if(taskId==null){
            taskId = choiceProject.getRandomTask().getId();
            this.choiceProject.put(choiceProject,taskId);
        }
        Task task1 = DailyTask.getTask(taskId);
        return task1;
    }

    public void save() throws SQLException, ClassNotFoundException {
        DailyTask.table.setJson(playerName,toJson().toString());
    }
}
